#include <iostream>

using namespace std;
#include <vector>
#include <algorithm>
#include <unordered_map>

class Solution {
public:
    int longestSubarray(vector<int>& nums) {
        int left = 0, right = 0;
        unordered_map<int, int> hash;
        int ret = 0;
        while (right < nums.size())
        {
            if (nums[right] != 1)
                hash[nums[right]]++;
            while (hash.size() > 1 || (nums[right] != 1 && hash[nums[right]] > 1))
            {
                if (nums[left] != 1)
                {
                    hash[nums[left]]--;
                    if (hash[nums[left]] == 0)
                        hash.erase(nums[left]);
                }
                left++;
            }
            ret = max(ret, right - left);
            right++;
        }
        return ret;
    }
};


int main()
{
    vector<int> nums = { 0,1,1,1,0,1,1,0,1 };
    Solution().longestSubarray(nums);
    return 0;
}